//-------------
var crypto = require('crypto'),
	User = require('../modles/user.js'),
	Post = require('../modles/post.js'),
	Comment = require('../modles/comment.js');
//------------- 

module.exports = function(app) {
	//主页
	app.get('/', function(req, res) {

		Post.getAll(null, function (err, posts) {
			if (err) {
				posts = [];
			}
			res.render('index', {
				title: '首页',
				user: req.session.user,
				posts: posts,
				success: req.flash('success').toString(),
				error:  req.flash('error').toString()
			});
		});
	});

	//登录页
	app.get('/login', checkNotLogin);
	app.get('/login', function(req, res) {
		res.render('login', {
			title: '登陆',
			user: req.session.user,
			success: req.flash('success').toString(),
			error:  req.flash('error').toString()
		});
	});

	app.post('/login', checkNotLogin);
	app.post('/login', function(req, res) {
		var md5 = crypto.createHash('md5'),
			password = md5.update(req.body.password).digest('hex');

		User.get(req.body.name, function (err, user) {
			if (!user) {
				req.flash("error", "该用户不存在");
				//用户不存在，返回登陆页面
				return res.redirect('/login');
			}

			if (user.password != password) {
				req.flash("error", "用户密码错误");
				//用户不存在，返回登陆页面
				return res.redirect('/login');
			}

			//验证成功
			//将用户信息存储于session之中
			req.session.user = user;

			req.flash("success", '登陆成功');
			//登陆成功，跳转主页页面
			res.redirect('/');
		});

	});

	//注册页面
	app.get('/reg', checkNotLogin);
	app.get('/reg', function(req, res) {
		res.render('reg', {
			title: '注册',
			user: req.session.user,
			success: req.flash('success').toString(),
			error:  req.flash('error').toString()
		});
	});

	app.post('/reg', checkNotLogin);
	app.post('/reg', function(req, res) {
		var name = req.body.name,
			password = req.body.password,
			password_re = req.body['password-repead'];

		//检验两次输入密码是否一致
		if (password != password_re) {
			req.flash('error', '两次输入密码不一致!');
			//返回注册页
			return res.redirect('/reg');
		}

		//生成密码的md5值
		var md5 = crypto.createHash('md5'),
			password = md5.update(req.body.password).digest('hex');
		var newUser = new User({
			name: req.body.name,
			password: password,
			email: req.body.email
		});
		//检查用户名是否已经存在
		User.get(newUser.name, function(err, user) {
			if (err) {
				//注册失败，返回注册页面
				req.flash('error', err);
				return res.redirect('/reg');
			}
			if (user) {
				//用户已存在
				//返回注册页
				req.flash('error', '用户已存在！');
				return res.redirect('/login');
			}
			//如果不存在，新增用户
			newUser.save(function(err, user) {
				if (err) {
					//注册失败，返回注册页面
					req.flash('error', err);
					return res.redirect('/reg');
				}
				//将用户信息存储于session之中
				req.session.user = newUser;
				//注册成功，返回主页
				req.flash('success', '注册成功');
				res.redirect('/');
				
			});
		});
	});

	//发表页面
	app.get('/post', checkLogin);
	app.get('/post', function(req, res) {
		res.render('post', {
			title: '发表',
			user: req.session.user,
			success: req.flash('success').toString(),
			error:  req.flash('error').toString()
		});
	});

	app.post('/post', checkLogin);
	app.post('/post', function(req, res) {

		//当前的用户
		var currentUser = req.session.user,
			tags = [req.body.tag1, req.body.tag2, req.body.tag2];
			post = new Post(currentUser.name, req.body.title, tags, req.body.post);
		post.save(function(err) {
			if (err) {
				req.flash('error', err);
				return res.redirect('/');
			}

			req.flash('success', '发表成功！');
			res.redirect('/');
		});
	});

	// 登出页面
	app.get('/logout', checkLogin);
	app.get('/logout', function(req, res) {
		res.render('logout', {
			title: '登出',
			user: req.session.user,
			success: req.flash('success').toString(),
			error:  req.flash('error').toString()
		});

	});
	
	app.post('/logout', checkLogin);
	app.post('/logout', function(req, res) {
		req.session.user = null;
		req.flash('success', '登出成功');
		res.redirect('/');
	});

	//上传
	app.get('/upload', checkLogin);
	app.get('/upload', function(req, res) {
		res.render('upload', {
			title: '文件上传',
			user: req.session.user,
			success: req.flash('success').toString(),
			error:  req.flash('error').toString()
		});
	});

	app.post('/upload', checkLogin);
	app.post('/upload', function (req, res) {
		req.flash('success', '文件上传成功！');
		res.redirect('/upload');
	});

	//个人文章列表
	app.get('/u/:name', function(req, res) {
		User.get(req.params.name, function(err, user) {
			if (!user) {
				req.flash('error', '用户不存在！');
				//用户不存在的情况下跳转主页
				return res.redirect('/');
			}

			//查询，并返回该用户的所有文章
			Post.getAll(user.name, function(err, posts) {
				if (err) {
					req.flash('error', err);
					return res.redirect('/');
				}
				res.render('user', {
					title: user.name,
					posts: posts,
					user: req.session.user,
					success: req.flash('success').toString(),
					error:  req.flash('error').toString()
				});
			});
		});
	});

	//文章页面
	app.get('/u/:name/:day/:title', function(req, res) {
		Post.getOne(req.params.name, req.params.day, req.params.title, function(err, post) {
			if (err) {
				req.flash('error', err);
				return res.redirect('/');
			}
			res.render('article', {
				title: req.params.title,
				post: post,
				user: req.session.user,
				success: req.flash('success').toString(),
				error:  req.flash('error').toString()
			});
		});
	});

	app.post('/u/:name/:day/:title', function(req, res) {
		var date = new Date(),
			time = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + ' ' + (date.getHours() < 10 ? '0' + date.getHours() :  date.getHours()) + ':' + (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());

		var comment = {
			name: req.body.name,
			email: req.body.email,
			website: req.body.website,
			time: time,
			content: req.body.content
		};

		var newComment = new Comment(req.params.name, req.params.day, req.params.title, comment);

		newComment.save(function (err) {
			if (err) {
				req.flash('error', err);
				return res.redirect('back');
			}
			req.flash('success', '留言成功！');
			res.redirect('back');
		});
	});

	// 编辑页面
	app.get('/edit/:name/:day/:title', checkLogin);
	app.get('/edit/:name/:day/:title', function (req, res) {
		var currentUser = req.session.user;
		Post.edit(currentUser.name, req.params.day, req.params.title, function (err, post) {
			if (err) {
				req.flash('error', err);
				return res.redirect('back');
			}
			res.render('edit', {
				title: '编辑',
				post: post,
				user: req.session.user,
				success: req.flash('success').toString(),
				error:  req.flash('error').toString()
			});
		});
	});

	app.post('/edit/:name/:day/:title', checkLogin);
	app.post('/edit/:name/:day/:title', function (req, res) {
		var currentUser = req.session.user;
		Post.update(currentUser.name, req.params.day, req.params.title, req.body.post, function (err) {

			//跳转链接(文章页)
			var url  = encodeURI('/u/' + req.params.name + '/' + req.params.day + '/' + req.params.title);
			if (err) {
				req.flash('error', err);
				return res.redirect(url);
			}
			req.flash('success', '修改成功！');
			res.redirect(url);
		});
	});

	//删除一篇文章
	app.get('/remove/:name/:day/:title', checkLogin);
	app.get('/remove/:name/:day/:title', function (req, res) {
		var currentUser = req.session.user;
		Post.remove(currentUser.name, req.params.day, req.params.title, function (err) {
			if (err) {
				req.flash('error', err);
				return res.redirect('back');
			}
			req.flash('success', '删除成功！');
			res.redirect('/');

		});
	});

	//存档页面
	app.get('/archive', function(req, res) {
		Post.getArchive(function(err, posts) {
			if (err) {
				req.flash('error', err);
				return res.redirect('/');
			}

			res.render('archive', {
				title: "列表",
				posts: posts,
				user: req.session.user,
				success: req.flash('success').toString(),
				error:  req.flash('error').toString()
			});
		});
	});

	// 标签页面
	app.get('/tags', function (req, res) {
		Post.getTags(function (err, posts) {
			if (err) {
				req.flash('error', err);
			}
			res.render('tags', {
				title: "标签",
				posts: posts,
				user: req.session.user,
				success: req.flash('success').toString(),
				error:  req.flash('error').toString()
			});
		});
	});

	app.get('/tags/:tag', function (req, res) {
		Post.getTag(req.params.tag, function (err, posts) {
			if (err) {
				req.flash('error', err);
				return res.redirect('/');
			}
			res.render('tag', {
				title: "TAG:" + req.params.tag,
				posts: posts,
				user: req.session.user,
				success: req.flash('success').toString(),
				error:  req.flash('error').toString()
			});
		});
	});

	app.get('/search', function (req, res) {

		Post.search(req.query.keyword, function (err, posts) {
			if (err) {
				req.flash('error', req.query.keyword);
				return res.redirect('/');
			}

			res.render('search', {
				title: "SEARCH:" + req.query.keyword,
				posts: posts,
				user: req.session.user,
				success: req.flash('success').toString(),
				error:  req.flash('error').toString()
			});
		});
	});

	//未登录判断，出错的时候返回未登录提示
	function checkLogin (req, res, next) {
		if (!req.session.user) {
			req.flash('error', '未登录！');
			res.redirect('/login');
		}
		next();
	}
	//已登录判断，出错的时候返回已登陆
	function checkNotLogin (req, res, next) {
		if (req.session.user) {
			req.flash('error', '已登录');
			res.redirect('back');
		}
		next();
	}
};

